<template>
  <div></div>
</template>
<script>
  import {bindWeChat} from "@/api/shop/api";

  export default {
    name: "BinDing",
    data() {
      return {
        appid: '',
        openId: '',
        code: ''
      }
    },
    mounted() {

    },
    created() {
      if (this.getQueryVariable('code')) {
        this.code = this.getQueryVariable('code')
      }
      //获取code后调用
      //步骤1
      if (this.code !== '') {
        let userName = this.getQueryVariable('userName')
        bindWeChat({"userName": userName, "code": this.code}).then(response => {
          this.showMsg(response)
        })
      } else {
        this.getAppIdAndCode(window.location.href)
      }
    },
    methods: {
      showMsg(response){
        if (response.code !== 200) {
          this.$modal.msgError(response.msg);
        } else {
          this.$modal.msgSuccess('已申请绑定');
        }
      },
      getAppIdAndCode(redirectUri) {
        this.getConfigKey("GZH_APP_ID").then(response => {
          this.appid = response.msg;
          this.getCode(redirectUri)
        });
      },
      getCode(redirectUri) {
        let param = "appid=" + this.appid + "&redirect_uri=" + redirectUri + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        let url = "https://open.weixin.qq.com/connect/oauth2/authorize?" + param;
        window.location.href = url
      }
    }
  };
</script>
